/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package sfs2x.extension.fourinaline.src.main;

import com.smartfoxserver.bitswarm.sessions.ISession;
import com.smartfoxserver.v2.core.ISFSEvent;
import com.smartfoxserver.v2.core.SFSEventParam;
import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.entities.data.SFSArray;
import com.smartfoxserver.v2.exceptions.SFSErrorCode;
import com.smartfoxserver.v2.exceptions.SFSErrorData;
import com.smartfoxserver.v2.exceptions.SFSException;
import com.smartfoxserver.v2.exceptions.SFSLoginException;
import com.smartfoxserver.v2.extensions.BaseServerEventHandler;
import com.smartfoxserver.v2.extensions.ExtensionLogLevel;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author dodinhchien
 */
public class UserLoginEventHandler extends BaseServerEventHandler{

    @Override
    public void handleServerEvent(ISFSEvent event) throws SFSException {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        String username = (String) event.getParameter(SFSEventParam.LOGIN_NAME);
        Object password = event.getParameter(SFSEventParam.LOGIN_PASSWORD);
        
        ISession session = (ISession) event.getParameter(SFSEventParam.SESSION);
        
        try {
            try (Connection conn = getParentExtension().getParentZone().getDBManager().getConnection()) {
                PreparedStatement sql = conn.prepareStatement("SELECT id, password From members Where username = ?");
                sql.setString(1, username);
                
                ResultSet result = sql.executeQuery();
                
                SFSArray row = SFSArray.newFromResultSet(result);
                
                if(password.equals("")){
                    SFSErrorData data = new SFSErrorData(SFSErrorCode.LOGIN_BAD_PASSWORD);
                    data.addParameter(username);
                    throw  new SFSLoginException("You must enter a password.", data);
                }
                
                if(username.equals("achau") || username.equals("Admin")){
                    SFSErrorData errorData = new SFSErrorData(SFSErrorCode.LOGIN_BAD_USERNAME);
                    errorData.addParameter(username);
                    throw  new SFSLoginException("achau and Admin are not allowed in this Zone!", errorData);
                }
                
                User user = getApi().getUserByName(username);
                if(user != null){
                    SFSErrorData errorAllreadyLogonData = new SFSErrorData(SFSErrorCode.LOGIN_ALREADY_LOGGED);
                    throw  new SFSLoginException(username + " allready login!", errorAllreadyLogonData);
                }
                
                
                ISFSObject a = (ISFSObject) event.getParameter(SFSEventParam.LOGIN_IN_DATA);
                a.getInt("source");
                trace("aaaaaa " + a);
                trace("Login succesfull, joining room!");
            }
        } catch (SQLException e) {
            trace(ExtensionLogLevel.WARN, "sql failed: " + e.toString());
        }
        
    }
    
}
